/******************************************************************************
 * The contents of this file are subject to the   JDI License  Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Initial Developer of the Original Code is 陈慧然. Portions created by 
 * 陈慧然 are Copyright 陈慧然.
 * All Rights Reserved.
 *
 *              Contact john_holen@163.com for licensing alternatives.
 *
 *****************************************************************************/


package org.peony.client.web;

import java.util.HashMap;
import java.util.Map;

import org.peony.exceptions.RenderException;
import org.peony.standard.ui.SIClientAll;
import org.peony.standard.ui.SIComponent;
import org.peony.standard.ui.SIContainer;
import org.peony.standard.ui.SILayOut;
import org.peony.standard.unvarying.client.web.IdsAndCssClassName;
import org.peony.tools.StringUtil;

/**
 *  
 *
 *  @author     陈慧然
 *  @version    Id: WebLayOutFlowVertical.java, v 0.000 2008-4-23 下午12:46:18 陈慧然 Exp
 */
public class WebLayOutFlowHorizontal implements SILayOut{
	private static final String empty = StringUtil.empty;
	/**
	 * 布局位置－上部
	 */
	public static final String POSITION_LEFT = "PORTION_SIDEBAR_LEFT";
	/**
	 * 布局位置－中部
	 */
	public static final String POSITION_CENTER = "PORTION_CONTENT_CENTER";
	/**
	 * 布局位置－下部
	 */
	public static final String POSITION_RIGHT = "PORTION_SIDERBAR_RIGHT";
	
	/**
	 * 布局位置－页头，等价于 布局位置－上部
	 */
	public static final String PORTION_LEFTSIDEBAR = POSITION_LEFT;
	/**
	 * 布局位置－正文，等价于 布局位置－中部
	 */
	public static final String PORTION_CENTER = POSITION_CENTER;
	/**
	 * 布局位置－页脚，等价于 布局位置－下部
	 */
	public static final String PORTION_RIGHTSIDEBAR = POSITION_RIGHT;
	
	private static final String UICLASS = IdsAndCssClassName.CONTAINER_LAYOUT_WEBLAYOUTFLOWHORIZONTAL;

	private Map<String,SIClientAll> contains;
	
	public WebLayOutFlowHorizontal() {
		contains = new HashMap<String,SIClientAll>();
		contains.put(POSITION_LEFT, null);
		contains.put(POSITION_CENTER, null);
		contains.put(POSITION_RIGHT, null);
	}
	public void add(SIComponent component, String position) throws RenderException {
		clear(position);
		contains.put(position, component);
	}

	public void add(SIContainer container, String position) throws RenderException {
		clear(position);
		contains.put(position, container);
	}

	public boolean isEmpty(String position) {
		if(contains.get(position) == null)
			return true;
		else
			return false;
	}

	public void clear(String position) throws RenderException {
		checkPosition(position);
		contains.put(position, null);
	}

	public SIComponent getComponent(String position) throws RenderException {
		checkPosition(position);
		if(contains.get(position) instanceof SIComponent)
			return (SIComponent)contains.get(position);
		else
			throw new RenderException("No SIComponent on " + position + " in the layout.");
	}

	public SIContainer getContainer(String position) throws RenderException {
		checkPosition(position);
		if(contains.get(position) instanceof SIContainer)
			return (SIContainer)contains.get(position);
		else
			throw new RenderException("No SIContainer on " + position + " in the layout.");
	}

	public void interchange(String positionA, String positionB)
			throws RenderException {
		checkPosition(positionA);
		checkPosition(positionB);
		if(positionA.equals(positionB))
			return;
		SIClientAll a = contains.get(positionA);
		SIClientAll b = contains.get(positionB);
		contains.put(positionA, b);
		contains.put(positionB, a);
	}

	public void reset() {
		contains.put(PORTION_CENTER, null);
		contains.put(PORTION_LEFTSIDEBAR, null);
		contains.put(PORTION_RIGHTSIDEBAR, null);
	}
	
	public String toStandardString() throws RenderException{
		StringBuffer sb = new StringBuffer();
		sb.append("<div class=\"").append(UICLASS).append("\">");
		sb.append(contains.get(PORTION_CENTER)==null?empty:contains.get(PORTION_CENTER).toStandardString());
		sb.append("</div>");
		sb.append("<div class=\"").append(UICLASS).append("\">");
		sb.append(contains.get(PORTION_LEFTSIDEBAR)==null?empty:contains.get(PORTION_LEFTSIDEBAR).toStandardString());
		sb.append("</div>");
		sb.append("<div class=\"").append(UICLASS).append("\">");
		sb.append(contains.get(PORTION_RIGHTSIDEBAR)==null?empty:contains.get(PORTION_RIGHTSIDEBAR).toStandardString());
		sb.append("</div>");
		return sb.toString();
	}

	private void checkPosition(String position)  throws RenderException {
		if(position.equals(PORTION_RIGHTSIDEBAR) || position.equals(PORTION_LEFTSIDEBAR) || position.equals(PORTION_CENTER))
			;
		else
			throw new RenderException("No Position named " + position + " in the layout.");
	}
}
